home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-12-30 | 6.5 KB | 341 lines | [TEXT/PJMM] |
- program ArraySpeed;
-
- {$IFC UNDEFINED THINK_PASCAL}
- uses
- Types, ToolUtils;
- {$ENDC}
-
- const
- kArrSize = 20000;
- kNumLoops = 50;
- type
- IntPtr = ^Integer;
- var
- theByteArr: packed array[0..kArrSize] of Byte;
- {theIntArr: array[0..kArrSize] of Integer;}
-
- type
- BytesArr = packed array[0..kArrSize] of Byte;
- BytesPtr = ^BytesArr;
-
- var
- i: Longint;
- loop: Integer;
- p: Ptr;
- chp: BytesPtr;
-
- ip: IntPtr;
- startTicks, finalTicks: Longint;
- begin
- {$IFC UNDEFINED THINK_PASCAL}
- {$ELSEC}
- ShowText;
- {$ENDC}
-
- startTicks := TickCount;
- for loop := 1 to kNumLoops do
- for i := 0 to kArrSize do
- theByteArr[i] := Byte(i);
- finalTicks := TickCount;
- WriteLn('for[]: ', finalTicks - startTicks : 1, ' ticks.');
-
- startTicks := TickCount;
- for loop := 1 to kNumLoops do
- begin
- p := @theByteArr[0];
- for i := 0 to kArrSize do
- begin
- p^ := i;
- p := Ptr(Longint(p) + 1);
- end;
- end;
- finalTicks := TickCount;
- WriteLn('for ptr: ', finalTicks - startTicks : 1, ' ticks.');
-
- startTicks := TickCount;
- for loop := 1 to kNumLoops do
- begin
- i := kArrSize;
- while i > 0 do
- begin
- theByteArr[i] := i;
- i := i - 1;
- end;
- end;
- finalTicks := TickCount;
- WriteLn('while []: ', finalTicks - startTicks : 1, ' ticks.');
-
- startTicks := TickCount;
- for loop := 1 to kNumLoops do
- begin
- p := @theByteArr[0];
- i := kArrSize;
- while i > 0 do
- begin
- p^ := i;
- p := Ptr(Longint(p) + 1);
- i := i - 1;
- end;
- end;
- finalTicks := TickCount;
- WriteLn('while ptr: ', finalTicks - startTicks : 1, ' ticks.');
-
- startTicks := TickCount;
- for loop := 1 to kNumLoops do
- begin
- p := @theByteArr[0];
- i := kArrSize;
- while i > 2 do
- begin
- p^ := i;
- p := Ptr(Longint(p) + 1);
- i := i - 1;
- p^ := i;
- p := Ptr(Longint(p) + 1);
- i := i - 1;
- end;
- while i > 0 do
- begin
- p^ := i;
- p := Ptr(Longint(p) + 1);
- i := i - 1;
- end;
- end;
- finalTicks := TickCount;
- WriteLn('while ptr unroll 2: ', finalTicks - startTicks : 1, ' ticks.');
-
- startTicks := TickCount;
- for loop := 1 to kNumLoops do
- begin
- p := @theByteArr[0];
- i := kArrSize;
- while i > 4 do
- begin
- p^ := i;
- p := Ptr(Longint(p) + 1);
- i := i - 1;
- p^ := i;
- p := Ptr(Longint(p) + 1);
- i := i - 1;
- p^ := i;
- p := Ptr(Longint(p) + 1);
- i := i - 1;
- p^ := i;
- p := Ptr(Longint(p) + 1);
- i := i - 1;
- end;
- while i > 0 do
- begin
- p^ := i;
- p := Ptr(Longint(p) + 1);
- i := i - 1;
- end;
- end;
- finalTicks := TickCount;
- WriteLn('while ptr unroll 4: ', finalTicks - startTicks : 1, ' ticks.');
-
- startTicks := TickCount;
- for loop := 1 to kNumLoops do
- begin
- p := @theByteArr[0];
- i := kArrSize;
- while i > 8 do
- begin
- p^ := i;
- p := Ptr(Longint(p) + 1);
- i := i - 1;
- p^ := i;
- p := Ptr(Longint(p) + 1);
- i := i - 1;
- p^ := i;
- p := Ptr(Longint(p) + 1);
- i := i - 1;
- p^ := i;
- p := Ptr(Longint(p) + 1);
- i := i - 1;
- p^ := i;
- p := Ptr(Longint(p) + 1);
- i := i - 1;
- p^ := i;
- p := Ptr(Longint(p) + 1);
- i := i - 1;
- p^ := i;
- p := Ptr(Longint(p) + 1);
- i := i - 1;
- p^ := i;
- p := Ptr(Longint(p) + 1);
- i := i - 1;
- end;
- while i > 0 do
- begin
- p^ := i;
- p := Ptr(Longint(p) + 1);
- i := i - 1;
- end;
- end;
- finalTicks := TickCount;
- WriteLn('while ptr unroll 8: ', finalTicks - startTicks : 1, ' ticks.');
-
- startTicks := TickCount;
- for loop := 1 to kNumLoops do
- begin
- p := @theByteArr[0];
- i := kArrSize;
- while i > 16 do
- begin
- p^ := i;
- p := Ptr(Longint(p) + 1);
- i := i - 1;
- p^ := i;
- p := Ptr(Longint(p) + 1);
- i := i - 1;
- p^ := i;
- p := Ptr(Longint(p) + 1);
- i := i - 1;
- p^ := i;
- p := Ptr(Longint(p) + 1);
- i := i - 1;
- p^ := i;
- p := Ptr(Longint(p) + 1);
- i := i - 1;
- p^ := i;
- p := Ptr(Longint(p) + 1);
- i := i - 1;
- p^ := i;
- p := Ptr(Longint(p) + 1);
- i := i - 1;
- p^ := i;
- p := Ptr(Longint(p) + 1);
- i := i - 1;
- p^ := i;
- p := Ptr(Longint(p) + 1);
- i := i - 1;
- p^ := i;
- p := Ptr(Longint(p) + 1);
- i := i - 1;
- p^ := i;
- p := Ptr(Longint(p) + 1);
- i := i - 1;
- p^ := i;
- p := Ptr(Longint(p) + 1);
- i := i - 1;
- p^ := i;
- p := Ptr(Longint(p) + 1);
- i := i - 1;
- p^ := i;
- p := Ptr(Longint(p) + 1);
- i := i - 1;
- p^ := i;
- p := Ptr(Longint(p) + 1);
- i := i - 1;
- p^ := i;
- p := Ptr(Longint(p) + 1);
- i := i - 1;
- end;
- while i > 0 do
- begin
- p^ := i;
- p := Ptr(Longint(p) + 1);
- i := i - 1;
- end;
- end;
- finalTicks := TickCount;
- WriteLn('while ptr unroll 16: ', finalTicks - startTicks : 1, ' ticks.');
-
- startTicks := TickCount;
- for loop := 1 to kNumLoops do
- begin
- i := kArrSize;
- while i > 4 do
- begin
- theByteArr[i] := i;
- i := i - 1;
- theByteArr[i] := i;
- i := i - 1;
- theByteArr[i] := i;
- i := i - 1;
- theByteArr[i] := i;
- i := i - 1;
- end;
- while i > 0 do
- begin
- theByteArr[i] := i;
- i := i - 1;
- end;
- end;
- finalTicks := TickCount;
- WriteLn('while [] unroll 4: ', finalTicks - startTicks : 1, ' ticks.');
-
- startTicks := TickCount;
- for loop := 1 to kNumLoops do
- begin
- i := kArrSize;
- while i > 8 do
- begin
- theByteArr[i] := i;
- i := i - 1;
- theByteArr[i] := i;
- i := i - 1;
- theByteArr[i] := i;
- i := i - 1;
- theByteArr[i] := i;
- i := i - 1;
- theByteArr[i] := i;
- i := i - 1;
- theByteArr[i] := i;
- i := i - 1;
- theByteArr[i] := i;
- i := i - 1;
- theByteArr[i] := i;
- i := i - 1;
- end;
- while i > 0 do
- begin
- theByteArr[i] := i;
- i := i - 1;
- end;
- end;
- finalTicks := TickCount;
- WriteLn('while [] unroll 8: ', finalTicks - startTicks : 1, ' ticks.');
-
-
-
-
-
- startTicks := TickCount;
- for loop := 1 to kNumLoops do
- begin
- chp := BytesPtr(@theByteArr[0]);
- i := kArrSize;
- while i > 4 do
- begin
- chp^[0] := i;
- chp := @chp^[1];
- i := i - 1;
- chp^[0] := i;
- chp := @chp^[1];
- i := i - 1;
- chp^[0] := i;
- chp := @chp^[1];
- i := i - 1;
- chp^[0] := i;
- chp := @chp^[1];
- i := i - 1;
- end;
- while i > 0 do
- begin
- chp^[0] := i;
- chp := @chp^[1];
- i := i - 1;
- end;
- end;
- finalTicks := TickCount;
- WriteLn('while ptr @[] unroll 4: ', finalTicks - startTicks : 1, ' ticks.');
-
- {$IFC UNDEFINED THINK_PASCAL}
- {$ELSEC}
- WriteLn('Click to exit.');
- while not Button do
- ;
- {$ENDC}
- end.